package com.hb.anative

import android.graphics.Bitmap
import android.view.Surface

/**
 * @anchor: andy
 * @date: 2018-11-13
 * @description:
 */
class NativeWindowSample {
    companion object {
        init {
            System.loadLibrary("native-window")
        }

        val MSG_DECODER_INIT_ERROR = 0
        val MSG_DECODER_READY = 1
        val MSG_DECODER_DONE = 2
        val MSG_REQUEST_RENDER = 3
        val MSG_DECODING_TIME = 4

        val MEDIA_PARAM_VIDEO_WIDTH = 0x0001
        val MEDIA_PARAM_VIDEO_HEIGHT = 0x0002
        val MEDIA_PARAM_VIDEO_DURATION = 0x0003

        val VIDEO_RENDER_OPENGL = 0
        val VIDEO_RENDER_ANWINDOW = 1
        val VIDEO_RENDER_3D_VR = 2
    }

    interface EventCallback {
        fun onPlayerEvent(msgType: Int, msgValue: Float)
    }


    private var mNativePlayerHandle: Long = 0

    private var mEventCallback: EventCallback? = null

    fun GetFFmpegVersion(): String? {
        return native_GetFFmpegVersion()
    }

    fun init(url: String?, videoRenderType: Int, surface: Surface?) {
        mNativePlayerHandle = native_Init(url!!, videoRenderType, surface!!)
    }

    fun play() {
        native_Play(mNativePlayerHandle)
    }

    fun pause() {
        native_Pause(mNativePlayerHandle)
    }

    fun seekToPosition(position: Float) {
        native_SeekToPosition(mNativePlayerHandle, position)
    }

    fun stop() {
        native_Stop(mNativePlayerHandle)
    }

    fun unInit() {
        native_UnInit(mNativePlayerHandle)
    }

    fun setBitmap(bitmap: Bitmap){
        native_Bitmap(mNativePlayerHandle,bitmap)
    }
    fun addEventCallback(callback: EventCallback) {
        mEventCallback = callback
    }

    fun getMediaParams(paramType: Int): Long {
        return native_GetMediaParams(mNativePlayerHandle, paramType)
    }

    private fun playerEventCallback(msgType: Int, msgValue: Float) {
        if (mEventCallback != null) mEventCallback!!.onPlayerEvent(msgType, msgValue)
    }

    private external fun native_GetFFmpegVersion(): String?

    private external fun native_Init(url: String, renderType: Int, surface: Any): Long
    private external fun native_Play(playerHandle: Long)
    private external fun native_SeekToPosition(playerHandle: Long, position: Float)
    private external fun native_Pause(playerHandle: Long)
    private external fun native_Stop(playerHandle: Long)
    private external fun native_UnInit(playerHandle: Long)
    private external fun native_GetMediaParams(playerHandle: Long, paramType: Int): Long
    private external fun native_Bitmap(playerHandle: Long, bitmap: Bitmap)

    external fun stringFromJNI(): String

    /**
     * 绘制指定颜色背景
     *
     * @param surface
     * @param color
     */
    external fun drawColor(surface: Any, color: Int)

    /**
     * 绘制指定颜色背景
     *
     * @param surface
     * @param bitmap
     */
    external fun drawBitmap(surface: Any, bitmap: Any)

    external fun nativeBitmap(bitmap: Any): IntArray
    external fun nativeBitmapArray(arr: IntArray): ByteArray
    external fun nativeBitmapIntArray(arr: IntArray): IntArray
}